package com.qwt.part_time_api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author 君子慎独
 */
@Configuration
@EnableOpenApi
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(Environment environment) {

        //        设置要显示Swagger的生存环境
        Profiles of = Profiles.of("dev");
        //        判断当前是否处在要显示Swagger的环境中
        boolean swaggerEnabled = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 是否开启
                .enable(swaggerEnabled).select()
                // 扫描的路径包
                .apis(RequestHandlerSelectors.basePackage("com.qwt.part_time_api.controller"))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any()).build().pathMapping("/");
    }

    /**
     * 配置Swagger信息 == apiInfo
     */
    private ApiInfo apiInfo() {

        /*
         * 作者信息
         */
        Contact contact = new Contact("Tzeao",
                "https://gitee.com/Tzeao/part-time-api",
                "773238750@qq.com");
        return new ApiInfoBuilder()
                .title("校园管理兼职平台")
                .description("该项目是使用Java基于Spring Boot框架进行开发的")
                .termsOfServiceUrl("http://localhost:8886/")
                .contact(contact)
                .version("1.0")
                .build();
    }
}
